package com.wondersgroup.fjzlpt.tbxt.dao.impl;

import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.repository.sql.bean.SQLBean;
import com.quick.framework.boot.repository.sql.bean.page.PageQuerySQLBean;
import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.SqlBeanBaseJTDaoImpl;
import com.quick.framework.boot.repository.sql.jdbcTemplate.rs.ResultHandlerConstant;
import com.wondersgroup.fjzlpt.tbxt.dao.inter.wsj.WsjDao;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 数据访问层
 * Created by sanma on 2017/7/18.
 */
@Repository
public class WsjDaoImpl extends SqlBeanBaseJTDaoImpl implements WsjDao {

	
    @Override
    public JSONObject queryNBPageAll(Map<String, String> map) {
        StringBuffer sb = new StringBuffer("select * from V_TBXT_SHXXJL_NB a where a.YLJGID <> ? ");
        //StringBuffer sb = new StringBuffer("select * from V_TBXT_SHXXJL_NB a where 1 = 1  ");
        List<String> list = new ArrayList<>();
        list.add(map.get("PTJGID"));
        String value = map.get("NF");
        if (!StringUtils.isEmpty(value)){
            sb.append(" and a.NF like ? ");
            list.add(value+"%");
        }
        value = map.get("YLJGID");
        if (!StringUtils.isEmpty(value)){
            sb.append(" and a.YLJGID = ? ");
            list.add(value);
        }
        //报表状态
        value = map.get("ZT");
        if (!StringUtils.isEmpty(value)){
            sb.append(" and a.ZT = ? ");
            list.add(value);
        }
        //行政区划
        value = map.get("XZQH");
        if (!StringUtils.isEmpty(value)){
            sb.append(" and a.XZQH like ? ");
            list.add(value + "__");
        }

        //sb.append("and a.ZT <> 0 ");//暂存
        PageQuerySQLBean sbl = checkAndCreatePageQuerySQLBean(map, sb.toString(), " order by a.XGSJ desc,a.ZT,a.JGLB ", list.toArray());
        JSONObject jsonObject = super.queryPage(sbl,ResultHandlerConstant.JSONARRAY);
        return jsonObject;
    }

    @Override
    public JSONObject queryYBPageAll(Map<String, String> map) {
    	StringBuffer sb = new StringBuffer("select * from V_TBXT_SHXXJL_YB a where a.YLJGID <> ? ");
    	//StringBuffer sb = new StringBuffer("select * from V_TBXT_SHXXJL_YB a where 1 = 1 ");
        List<String> list = new ArrayList<>();
        list.add(map.get("PTJGID"));
        String value = map.get("YF");
        if (!StringUtils.isEmpty(value)){
            sb.append(" and a.Yf = ? ");
            list.add(value);
        }
        value = map.get("YLJGID");
        if (!StringUtils.isEmpty(value)){
            sb.append(" and a.YLJGID = ? ");
            list.add(value);
        }
        value = map.get("ZT");
        if (!StringUtils.isEmpty(value)){
            sb.append(" and a.zt = ? ");
            list.add(value);
        }
        value = map.get("XZQH");
        if (!StringUtils.isEmpty(value)){
            sb.append(" and a.XZQH like ? ");
            list.add(value + "__");
        }
        //sb.append("and a.ZT <> 0 ");//暂存
        PageQuerySQLBean sbl = checkAndCreatePageQuerySQLBean(map, sb.toString(), " order by a.XGSJ desc,a.ZT,a.JGLB ", list.toArray());
        JSONObject jsonObject = super.queryPage(sbl,ResultHandlerConstant.JSONARRAY);
        return jsonObject;
    }

    /**
     * 更新报表状态
     * @param map
     * @return
     */
    @Override
    public void updateBbZtByTableName(Map<String,String> map ){
    	String tableName = map.get("tableName");
    	String ZT = map.get("ZT");
    	String ID = map.get("ID");
    	if(!StringUtils.isEmpty(tableName) && !StringUtils.isEmpty(ZT) && !StringUtils.isEmpty(ID)){
            tableName = "TB_TBXT_" + tableName;
    		StringBuffer sqlBuf = new StringBuffer("update ").append(tableName).append(" set ZT = ? where ID = ?");
    		SQLBean bean = new SQLBean(sqlBuf.toString(), ZT, ID);
    		super.update(bean);
    	}else{
    		throw new RuntimeException("更改报表状态出现参数异常");
    	}
    	
    	
    }
    
    
}
